#!/bin/bash

USAGE="`basename $0` [-w|--warning]"

if [ $# -ne 2 ];then
    echo -e "\nWrong Syntax:`basename $0` $*"
    echo -e "\nUsage: $USAGE\n"
    exit 2
fi

case "$1" in
    -w|--warning)
        shift
        WAR=$1
        ;;
    *)
        echo -e "\nWrong Syntax:`basename $0` $*"
        echo -e "\nUsage: $USAGE\n"
        exit 2
esac

total=`grep "^MemTotal" /proc/meminfo |awk '{print $2}'`
free=`grep "^MemFree" /proc/meminfo |awk '{print $2}'`
buffer=`grep "^Buffers" /proc/meminfo |awk '{print $2}'`
cache=`grep "^Cached" /proc/meminfo |awk '{print $2}'`
use=$[$total-$free-$buffer-$cache]

lv=$[$use*100/$total]

echo "Total is $[$total/1024]MB,Use is $[$use/1024]MB,Free is $[$free/1024]MB,Buffers is $[$buffer/1024]MB,Cached is $[$cache/1024]MB,Percent use is $lv%|total:$[$total*1024]:0;use:$[$use*1024]:0;buffers:$[$buffer*1024]:0;cached:$[$cache*1024]:0;free:$[$free*1024]:0"

if `echo $WAR|grep -q '%'`;then
    WAR=`echo $WAR|cut -d'%' -f1`
    if [ $lv -lt $WAR ];then
        exit 0
    else
        exit 1
    fi
else
    WAR=$[$WAR*1024]
    if [ $use -lt $WAR ];then
        exit 0
    else
        exit 1
    fi
fi
